<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>ARC Scheduler</title>
  <%= stylesheet_link_tag 'search' %>
  <%= stylesheet_link_tag 'user' %>

  <!-- All common javascript functions will go here -->
  <%=  javascript_include_tag 'prototype' %>
  <script type="text/javascript">
	function getMore(num){
		new Ajax.Updater({success: 'search_result', failure: 'statusPanel'}, '/plusResults', {
			evalScripts: true,
			insertion: Insertion.Bottom,
			parameters: {
				moreCount: num
			}
		});
	};
	
	function saveLecture(myCcn){
		var isChecked = document.getElementById('c' + myCcn + '_checkbox').checked;
		new Ajax.Request('/save_class', {
			parameters: {
				ccn: myCcn,
				save: isChecked
			},
			onSuccess: function(transport){
				var data = transport.responseText.evalJSON();
				if (data['status'] == true){
					var toDo = data['alternative_lectures'];
					for (var i = 0; i < toDo.length; i++){
						var ccn = toDo[i];
						if (ccn != "0") {
							document.getElementById('c' + ccn + '_checkbox').checked = isChecked;
						}
					}
					showSuccess("Request Completed for " + myCcn);
				}
				else {
					showError("Request failed: " + data['error']);
					revertState(myCcn, isChecked);
					
				}
			},
			onFailure: function(){
				showError("Request to server failed!");
				revertState(myCcn, isChecked);
			}
		});
	}
	
	function revertState(myCcn, state) {
		document.getElementById('c' + myCcn + '_checkbox').checked = !state;
	}
	
	function showSuccess(message){
		var panel = document.getElementById("statusPanel");
		panel.innerHTML = message;
		panel.className = 'index-statusPanel-success'
		panel.style.visibility = 'visible';
		setTimeout("hideBar()",5000);
	}
	
	function showError(message){
		var panel = document.getElementById("statusPanel");
		panel.innerHTML = message;
		panel.className = 'index-statusPanel-failure'
		panel.style.visibility = 'visible'; 
		setTimeout("hideBar()",10000);
	}
	
	function hideBar(){
		document.getElementById("statusPanel").style.visibility = 'hidden';
	}
  </script>  
</head>
<body>
<div id="container">
<%= render :partial => "users/user_bar" %>
<%= render :partial => 'search_bar' %>

	<!-- Status Panel holds text if there were any errors with the request -->
	<div id="statusPanel" style="visibility:hidden"></div>

	<!-- now render the results -->
	<div id="search_result">
		<% if @events.empty? %>
<%= render :partial => 'empty_result' %>
		<% else %>
<%= render :partial => 'lecture', :collection => @events %>

<!-- Once lectures are all loaded, hide list of discussions and labs
This needs to be done by JavaScript or otherwise -->
<script type="text/javascript">

    elems = $$('div.discussions')
    for (var i = 0; i < elems.length; i++)
        elems[i].hide();

    elems = $$('div.labs')
    for (var i = 0; i < elems.length; i++)
        elems[i].hide();

</script>
		<% end %>
	</div>

	<% if not @events.empty? %>
	<div id="more_results">
<%
    total_pages = (@nav_total_events + (@nav_limit-1)) / @nav_limit
    current_page = @nav_offset / @nav_limit
    start = [0, current_page-5].max
    stop = [total_pages-1, current_page+5].min
    escaped_query = URI.escape(params[:query])
%>[<%
    (start..stop).each do |i|
        if i == current_page
%>      <%= i+1 %>
<% else
%>      <%= link_to i+1, "/?query=#{escaped_query}&offset=#{i*@nav_limit}&limit=#{@nav_limit}" %>
<%      end
    end
    if stop < total_pages-1
        %>... <%
    end
%>]<br/>
    (<%= current_page+1 %> of <%= total_pages %>)
	</div>
	<% end %>
</div>
</body>
</html>
